package cn.yitun.chat.yitunserverimcore.general.main.push.remote;

import cn.yitun.chat.yituncommonmessage.impl.HeadImpl;
import cn.yitun.chat.yituncommonmessage.push.PushMessage;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.enums.control.push.remote.RemotePushMethodEnum;
import cn.yitun.chat.yitunserverimcore.bridge.net.WriteHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class RemotePush {
	//1.5.004
	final String action = ActionTypeConstant.REMOTE_ACTION_PUSH;

	@Resource
	WriteHandler writeHandler;

	/**
	 * 服务器收到客户端后
	 * 
	 * @param userId
	 */
	public void pushReceivedRemoteAddress(Long userId) {
		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(RemotePushMethodEnum.RECEIVED_REMOTE_ADDRESS.getMethod());
		head.setTimestamp(System.currentTimeMillis());
		PushMessage message = new PushMessage();
		message.setHead(head);
		writeHandler.push(userId, message);
	}

	/**
	 * 向远程控制器发请求
	 * @param key
	 * @param receiveUserId
	 * @param sendUserId
	 */
	public void pushRequestRemoteControl(Long key, Long receiveUserId, Long sendUserId) {
		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(RemotePushMethodEnum.REQUEST_REMOTE_CONTROL.getMethod());
		head.setTimestamp(System.currentTimeMillis());
		head.setKey(key);

		PushMessage message = new PushMessage();
		message.setHead(head);
		message.bodyPut("receiveUserId", receiveUserId);
		message.bodyPut("sendUserId", sendUserId);
		writeHandler.push(receiveUserId, message);
	}

	/**
	 * 向远程控制器发响应
	 * @param key
	 * @param sendUserId
	 * @param receiveUserId
	 * @param actionType
	 * @param code
	 * @param message
	 */
	public void pushResponseRemoteControl(Long key, Long sendUserId, Long receiveUserId, String actionType, String code, String message) {
		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(RemotePushMethodEnum.RESPONSE_REMOTE_CONTROL.getMethod());
		head.setTimestamp(System.currentTimeMillis());
		head.setKey(key);

		PushMessage m = new PushMessage();
		m.setHead(head);
		m.put("receiveUserId", receiveUserId);
		m.put("sendUserId", sendUserId);
		m.put("actionType", actionType);
		m.put("code", code);
		m.put("message", message);
		writeHandler.push(receiveUserId, m);
	}

	/**
	 * 向远程助理发请求
	 * @param key
	 * @param receiveUserId
	 * @param sendUserId
	 */
	public void pushRequestRemoteAssist(Long key, Long receiveUserId, Long sendUserId) {
		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(RemotePushMethodEnum.REQUEST_REMOTE_ASSIST.getMethod());
		head.setTimestamp(System.currentTimeMillis());
		head.setKey(key);

		PushMessage message = new PushMessage();
		message.setHead(head);
		message.bodyPut("receiveUserId", receiveUserId);
		message.bodyPut("sendUserId", sendUserId);
		writeHandler.push(receiveUserId, message);
	}

	/**
	 * 向远程助理发响应
	 * @param key
	 * @param sendUserId
	 * @param receiveUserId
	 * @param actionType
	 * @param code
	 * @param message
	 */
	public void pushResponseRemoteAssist(Long key, Long sendUserId, Long receiveUserId, String actionType, String code, String message) {
		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(RemotePushMethodEnum.RESPONSE_REMOTE_ASSIST.getMethod());
		head.setTimestamp(System.currentTimeMillis());
		head.setKey(key);

		PushMessage m = new PushMessage();
		m.setHead(head);
		m.put("receiveUserId", receiveUserId);
		m.put("sendUserId", sendUserId);
		m.put("actionType", actionType);
		m.put("code", code);
		m.put("message", message);
		writeHandler.push(receiveUserId, m);
	}

	/**
	 * 解除远程控制
	 * @param key
	 * @param receiveUserId
	 * @param sendUserId
	 */
	public void pushReleaseRemoteControl(Long key, Long receiveUserId, Long sendUserId) {
		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(RemotePushMethodEnum.RELEASE_REMOTE_CONTROL.getMethod());
		head.setTimestamp(System.currentTimeMillis());
		head.setKey(key);

		PushMessage message = new PushMessage();
		message.setHead(head);
		message.bodyPut("receiveUserId", receiveUserId);
		message.bodyPut("sendUserId", sendUserId);
		writeHandler.push(receiveUserId, message);
	}

}
